<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<!doctype html>

<html class="no-js">
<style type="text/css">
.am-radius {
	color: gray;
	padding-right: 1%;
}
</style>
<head>
<%@ include file="./commoms/link.txt"%>
<script src="assets/js/pagejs/EEIndex.js"></script>
</head>
<body>
	<%@ include file="./commoms/userHeader.txt"%>
	<div class="admin-content">
		<div class="admin-content-body">
			<div class="am-container" style="margin-top: 40px;">
			

				<div id="pagecontainer"></div>
	   <div class="am-panel am-panel-default "  >
				<div class="am-panel-hd"  >
					<h2 class="am-panel-title ">
						<strong>位置信息</strong>
					</h2>
				</div>

				<div class="am-panel-bd">
					
						<div id="container" style="height: 400px; border: 1px solid gray"
							></div>
			
<div id="driving_way">
		<select>
			<option value="0">最少时间</option>
			<option value="1">最短距离</option>
			<option value="2">避开高速</option>
		</select>
	  <!--  <input type="text" id="device"   disabled="disabled"/>-->	
		<input type="button" id="result" value="查询" disabled="disabled"/>
	</div>
	<div id="r-result"></div>
				</div>

			</div>

			</div>


		</div>
	
	</div>
	</div>
<script src="assets/js/pagejs/EEIndex.js"></script>
<script type="text/javascript">
var sosiid=<%=request.getParameter("sosiid")%>;
if(sosiid==null&&typeof(sosiid) != "undefined")
	{
	
	window.location.href=domain+"/userIndex.jsp";
	}
	
var detail=null;
var myGeo = new BMap.Geocoder();
var map = new BMap.Map("container"); // 创建地图实例
	var longitude=116.331398;
	 var latitude=39.897445;
var point = new BMap.Point(longitude,latitude );
map.centerAndZoom(point, 12);
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl());
map.addControl(new BMap.MapTypeControl());
var geolocation = new BMap.Geolocation();
var myposition=null;
var output = "从从您现在的位置到孩子位置需要";

var mychildposition=[];
getall();	
	
function getall(){
	
	map.clearOverlays(); 
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
			
			 var convertor = new BMap.Convertor();
		        var pointArr = [];
		        pointArr.push(r.point);
		        convertor.translate(pointArr, 1, 5, function (data){
		       	 if(data.status === 0) {
		       		 var point=data.points[0];
		    	        var marker = new BMap.Marker(point);
		    	        map.addOverlay(marker);
		    	        var label = new BMap.Label("您的位置",{offset:new BMap.Size(20,-10)});
		    	        marker.setLabel(label); //添加百度label
		    	        map.setCenter(point);
		              	map.panTo(point);
		              	myposition=point;
		              	marker.addEventListener("click", function(){    
		              		 getGeoAddress(point)  
		              		});
		                if(mychildposition[0]!=null && typeof(mychildposition[0])!='undefined')
			    	        $('#result').removeAttr("disabled");
		    	      }
		       	 });
		}
		else {
			alert('failed'+this.getStatus());
		}        
	},{enableHighAccuracy: true});

	
	myAjax(domain + "/api/sos/getsosdetail.do", {
		"sosiid":sosiid
		
	}, function(data) {
		if (data.code == 200) {
	    if(data.result!=null)
	    	{
	    	 
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    
	    	
	    	
	    	
	    	detail=data.result;
	    	// sosiid=detail[0].sosiid;
	    	 if(detail!=null||typeof(detail)!='undefined')
	    		{
	    		 detail=data.result;
	    			if(detail!=null&&typeof(detail) != "undefined")
       			{
	    				 var convertor = new BMap.Convertor();
	    		 	        var pointArr = [];
	    		 	       
	    			      	mychildposition[0]=new BMap.Point(detail.longtitude,detail.latitude);
	    			    	
	    			      	pointArr.push(mychildposition[0]);
	    			      	
	    			      	
	    			        convertor.translate(pointArr, 1, 5, function (data){
	    				       	   for (var i = 0; i < data.points.length; i++) {
	    				               var point=data.points[i];
	    				               mychildposition[i]=point;
	    				    	        var marker = new BMap.Marker(point);
	    				    	      
	    				    	        map.addOverlay(marker);
	    				    	        var label = new BMap.Label(detail.device.holder+"的位置",{offset:new BMap.Size(20,-10)});
	    				    	        marker.setLabel(label); //添加百度label
	    				    	      //  search(start,end,routePolicy[i]);
	    				    	        marker.addEventListener("click", function(){    
	    				              		 getGeoAddress(point)  
	    				              		});
	    				    	        if(myposition!=null && typeof(myposition)!='undefined')
	    				    	        $('#result').removeAttr("disabled");
	    				    	        
	    				    	      }
	    				       	 });
	    			        	   
       			}
	    		 
	    		}
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	 
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	
	    	}
	
	   }
		else 	if (data.code == 406){
			myAlert("警告", "参数不对");
		}
		
	});
	
	
	

	
}
	
	
	
	
	
	
	
	
	
	
	

	  
	    


/*
geolocation.getCurrentPosition(function(r){
	if(this.getStatus() == BMAP_STATUS_SUCCESS){
		var mk = new BMap.Marker(r.point);
		map.addOverlay(mk);
		map.panTo(r.point);
		myposition=r.point;
	}
	else {
		alert('failed'+this.getStatus());
	}        
},{enableHighAccuracy: true})*/

	function search(start,end,route){ 
		var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true,panel: "r-result"},policy: route
			,onSearchComplete: function (results){
				if (driving.getStatus() != BMAP_STATUS_SUCCESS){
					return ;
				}
			
				var plan = results.getPlan(0);
				output='';
				output += plan.getDuration(true) + "\n";                //获取时间
				output += "总路程为：" ;
				output += plan.getDistance(true) + "\n";             //获取距离
			},
			onPolylinesSet: function(){        
				setTimeout(function(){alert(output)},"1000");
		}});
		driving.search(start,end);
	}
	function getGeoAddress(point) {
		myGeo.getLocation(point,
				function(result) {
					if (result) {
						geoaddress = result.address;
						lat = point.lat;
						lng = point.lng;
						infoWindow = new BMap.InfoWindow(geoaddress, opts); // 创建信息窗口对象
						map.openInfoWindow(infoWindow,point);
					}
				});

	}
	var opts = {
			width : 250, // 信息窗口宽度
			height : 100, // 信息窗口高度
			title : "地址" // 信息窗口标题
		}
	
	
	
	
	
	var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
	$("#result").click(function(){
		map.clearOverlays(); 
		var start=myposition,end=null;
//		var holder=sosiid;
		var i=$("#driving_way select").val();
	if(mychildposition!=null && typeof(mychildposition)!='undefined')
	{
		if(mychildposition[0]!=null && typeof(mychildposition[0])!='undefined')
		{
            	 end=mychildposition[0];
            
            	 
            	 if(myposition!=null && typeof(myposition)!='undefined')
         		{
            		 start=myposition
            		 
            		 search(start,end,routePolicy[i]); 
         		}
            	 
	     }
     }

	});

</script>

</body>

<a href="#"
	class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu"
	data-am-offcanvas="{target: '#admin-offcanvas'}"></a>
</html>
